#include<graphics.h>
#include<conio.h>
#include<math.h>
#define High 480
#define Width 640
#define PI 3.1415926535
int main()
{
	initgraph(Width, High);
	int center_x, center_y;
	center_x = Width / 2;
	center_y = High / 2;
	int secLength;
	int minLength;
	int houLength;
	secLength = Width / 5;
	minLength = Width / 8;
	houLength = Width / 10;
	int secEnd_x, secEnd_y;
	int minEnd_x, minEnd_y;
	int houEnd_x, houEnd_y;
	float secAngle;  //秒针角度
	float minAngle;  //分针角度
	float houAngle;  //时针角度
	SYSTEMTIME it;  //定义变量存储系统时间
	BeginBatchDraw();
	while (1)
	{
		//绘制表盘
		int radius = Width / 4;
		setlinestyle(PS_SOLID, 1);
		setcolor(WHITE);
		circle(center_x, center_y, radius);
		//刻度
		int x, y, i;
		for (i = 0; i < 60; i++)
		{
			x = center_x + int(Width / 4.3*sin(PI * 2 * i / 60));
			y = center_y + int(Width / 4.3*cos(PI * 2 * i / 60));
			if (i % 15 == 0)
				bar(x - 5, y - 5, x + 5, y + 5);
			else if (i % 5 == 0)
				circle(x, y, 3);
			else
				putpixel(x, y, WHITE);
		}
		//获取当前时间
		GetLocalTime(&it);
		secAngle = it.wSecond * 2 * PI / 60;   //秒针转动角度
		minAngle = it.wMinute * 2 * PI / 60 + secAngle / 60;    //分钟转动角度
		houAngle = it.wHour * 2 * PI / 12 + minAngle / 12;    //时针转动角度
		secEnd_x = center_x + secLength * sin(secAngle);
		secEnd_y = center_y - secLength * cos(secAngle);
		minEnd_x = center_x + minLength * sin(minAngle);
		minEnd_y = center_y + minLength * cos(minAngle);
		houEnd_x = center_x + houLength * sin(houAngle);
		houEnd_y = center_y + houLength * cos(houAngle);
		//画秒针
		setlinestyle(PS_SOLID, 2);
		setcolor(YELLOW);
		line(center_x, center_y, secEnd_x, secEnd_y);
		//画分针
		setlinestyle(PS_SOLID, 3);
		setcolor(RED);
		line(center_x, center_y, minEnd_x, minEnd_y);
		//画时针
		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(center_x, center_y, houEnd_x, houEnd_y);
		FlushBatchDraw();
		//隐藏前一帧
		setcolor(BLACK);
		line(center_x, center_y, secEnd_x, secEnd_y);
		setcolor(BLACK);
		line(center_x, center_y, minEnd_x, minEnd_y);
		setcolor(BLACK);
		line(center_x, center_y, houEnd_x, houEnd_y);
	}
	EndBatchDraw();
	_getch();
	closegraph();
	return 0;
}
